from flask import Blueprint, jsonify, request, redirect
from application.models.users.user import *
from application.tools.myredis import r

course_blue = Blueprint("courseblue", __name__, url_prefix="/course")


@course_blue.route("/getDetail")
def getDetail():
    # 获取id
    id = request.args.get('id')
    userid = request.args.get('userid')
    # 查询课程信息
    course = Course.query.filter(Course.id==id).first()
    # 加载浏览记录，获取userid
    if userid:
        key = "collection"+str(userid)+'pro'+str(id)
        r.hash_add(key,'courseid',course.id)
        r.hash_add(key,'name',course.name)
    # 返回课程信息
    course = {"id":course.id,'name':course.name}
    return jsonify({"code":200,'course':course})


@course_blue.route("/getViews")
def getViews():
    userid = request.args.get('userid')
    findkey = "collecion"+str(userid)+"*"
    gidlist = r.getvalues(findkey)
    reslist = []
    for i in gidlist:
        goods = r.getall_hash(i)
        dict = {}
        for k,v in goods.items():
            kk = k.decode()
            vv = v.decode()
            dict[kk] = vv
        reslist.append(dict)
    return jsonify({"code":200,'lisyt':reslist})

# 加入购物车
@course_blue.route("/buy")
def buy():
    # 获取参数 userid courseid
    userid = request.args.get('userid')
    courseid = request.args.get('courseid')
    # 从hash中获取数据，如果存在数量+1 buy1course2
    key = "buy"+str(userid)+'course'+str(courseid)
    # 如果不存在存入redis hash
    flag = r.findkey_hash(key)
    print(flag)
    # 存在
    if flag:
    #     数量+1
        r.jiajian_hash(key,'number',1)
    else:
        r.hash_add(key,'id',courseid)
        r.hash_add(key,'number',1)
    return jsonify({"code":200})
    # 返回结果


@course_blue.route("/mycart")
def mycart():
    # 获取参数userid
    userid = request.args.get('userid')
    # 从hash中获取商品id和数量
    key = 'buy'+str(userid)+"*"
    goods = r.getvalues(key)
    glist=[]
    tmoney=0
    tcount=0
    if goods:
        for i in goods:
            # 获取每一条商品的具体信息
            good = r.getall_hash(i)
            rescourse={}
            for k,v in good.items():
                params = k.decode()
                value = v.decode()
                print(value)
                if params == 'id':
                    # 获取到的商品信息
                    tcourse = Tcourse.query.filter(Tcourse.id == value).first()
                    rescourse['id'] = tcourse.id
                    rescourse['title'] = tcourse.title
                    rescourse['url'] = tcourse.url
                    rescourse['price'] = float(tcourse.price)
                if params == 'number':
                    # 获取购买的数量
                    rescourse['number'] = value
            rescourse['total'] = float(rescourse['price'])*float(rescourse['number'])
            # 计算总价格
            tmoney=float(tmoney)+float(rescourse['total'])
            tcount = int(tcount)+int(rescourse['number'])
            glist.append(rescourse)
    # 价格、图片、名字。。。。从mysql中获取，从redis商品缓存中读取
    # 数据重组返回
    return jsonify({"code":200,'glist':glist,'tmoney':tmoney,'tcount':tcount})